using System.Windows.Forms;
using Krile.Cores;
using Krilib;

namespace Krile
{
    /// <summary>
    /// System Core
    /// </summary>
    internal static class Core
    {
        /// <summary>
        /// Initialize core
        /// </summary>
        internal static void Init()
        {
            //Core data initialize
            Define.AppPath = Application.StartupPath;
            //Core value initialize
            formmanager = new FormManager();
            pluginmanager = new PluginManager();
            imagemanager = new ImageManager();
            statusmanager = new StatusManager();
            controlmanager = new ControlManager();
            keyassign = new KeyAssign();
            notificationmanager = new NotificationManager();

            //System initialize
            Subsystem.SetGlobal.SetDefaultSearchDatas();
            Subsystem.SetGlobal.SetDefaultKeyAssigns();

            //Load Configuration should be put LAST.
            config = Config.Load();

        }

        #region Core Values

        private static FormManager formmanager;
        /// <summary>
        /// Form manager
        /// </summary>
        internal static FormManager FormManager { get { return formmanager; } }

        private static PluginManager pluginmanager;
        /// <summary>
        /// Plugin manager
        /// </summary>
        internal static PluginManager PluginManager { get { return pluginmanager; } }

        private static Config config;
        /// <summary>
        /// Configuration
        /// </summary>
        internal static Config Config { get { return config; } }

        private static ImageManager imagemanager;
        /// <summary>
        /// Image manager
        /// </summary>
        internal static ImageManager ImageManager { get { return imagemanager; } }

        private static StatusManager statusmanager;
        /// <summary>
        /// Status manager
        /// </summary>
        internal static StatusManager StatusManager { get { return statusmanager; } }

        private static ControlManager controlmanager;
        /// <summary>
        /// Control manager
        /// </summary>
        internal static ControlManager ControlManager { get { return controlmanager; } }

        private static KeyAssign keyassign;
        /// <summary>
        /// Key assign controller
        /// </summary>
        internal static KeyAssign KeyAssign { get { return keyassign; } }

        private static NotificationManager notificationmanager;
        /// <summary>
        /// Notification manager
        /// </summary>
        internal static NotificationManager NotificationManager { get { return notificationmanager; } }

        #endregion

        private static ConfigBridge cbridge;
        private static Subsystem.__DebugBridge dbridge;
        private static Subsystem.__CommmonOperationBridge cobridge;
        public static void SetLibBridge()
        {
            cbridge = new ConfigBridge();
            Bridge.Config = cbridge;
            Bridge.ControlManager = controlmanager;
            dbridge = new Krile.Subsystem.__DebugBridge();
            Bridge.DebugReporter = dbridge;
            Bridge.ImageManager = imagemanager;
            Bridge.MainForm = formmanager.MainForm;
            Bridge.PluginManager = pluginmanager;
            Bridge.StatusManager = statusmanager;
            cobridge = new Krile.Subsystem.__CommmonOperationBridge();
            Bridge.CommonOperation = cobridge;
            Bridge.NotificationManager = notificationmanager;
        }

        /// <summary>
        /// Exit krile
        /// </summary>
        public static void ExitApp()
        {
            SaveConfig();
            PluginManager.EndPlugin();
            Application.Exit();
        }

        /// <summary>
        /// Save configuration to disk
        /// </summary>
        public static void SaveConfig()
        {
            Core.Config.State.TabDatas = Core.FormManager.MainForm.TabDatas;
            Core.Config.Save();
        }
    }
}
